<?php

/**
 * 
 * 首页格子模型
 */
class GeziModel extends Model {

    private $article, $link;

    public function _initialize() {
        $this->article = M('PayPage');
        $this->link = D('PayLink');
    }

    public function fabu($param) {
        M()->startTrans();
        //是否有链接
        if ($param['link_status'] == 1) {
            $link_id = $this->link->newLink($param['link'], $param['link_days']);
            if ($link_id == false) {
                M()->rollback();
                return reStatus(false, '链接发布失败');
            }
        }
        //是否有次级页面
        if ($param['page_status'] == 1) {
            if ($param['page_id']) {
                $payPage = $this->article->where(array('page_id'                  => $param['page_id']))->find();
                $payPage['con_type']       = 'gezi';
                $payPage['star_time']      = time();
                $payPage['end_time']       = time() + 24 * 3600 * $param['page_days'];
                $payPage['days']           = $param['page_days'];
                $payPage['article_status'] = 1;
                $pagStatus                 = $this->article->save($payPage);
                if ($pagStatus == false) {
                    M()->rollback();
                    return reStatus(false, '次级页面修改错误');
                }
            } else {
                M()->rollback();
                return reStatus(false, '次级页面发布有误');
            }
        }
        $gezi = array(
            'user_id'      => session('uid'),
            'title'        => t($param['content']),
            'content'      => t($param['content']),
            's_img'        => t($param['s_img']),
            'b_img'        => ($param['img_zoom'] == 1) ? $param['b_img'] : 0,
            'img_zoom'     => $param['img_zoom'] ? 1 : 0,
            'link_id'      => $param['link_status'] ? $link_id : 0,
            'link_days'    => $param['link_days'],
            'article_id'   => $param['page_id'],
            'article_days' => $param['page_days'],
            'star_time'    => time(),
            'end_time'     => time() + 24 * 3600 * (int) $param['days'],
            'days'         => $param['days'],
            'order_status' => 1,
            'status'       => 1,
            'pay_score'    => $param['pay_score'],
            'create_time'  => time(),
        );
        $geziID        = $this->add($gezi);
        if ($geziID) {
            M()->commit();
            return reStatus(true, '格子发布成功, 需要后台审核', $geziID);
        } else {
            M()->rollback();
            return reStatus(false, '格子发布失败');
        }
    }

    public function show($limit = 10) {
        $where = array(
            'end_time' => array('EGT', time()),
            'status' => 1,
        );
        $list    = $this->where($where)->order($order)->limit($limit)->findPage($limit);
        dump($list);
    }

}

?>
